package work.timejay.user.utils;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.UUID;

/**
 * @className:WebUtils.java
 * @author:王文杰
 * @version:1.0.0
 * @description:TODO
 * @createTime 2022年04月17日 14:22:00
 */
@Slf4j
public class WebUtils {
    /**
     * 获取code
     */
    public static String getCode() throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        // HttpGet get = new HttpGet("https://open.weixin.qq.com/connect/qrconnect?appid=wxbbe8b9496e8ed462&redirect_uri=https://auvg7x.39nat.com/web&response_type=code&scope=snsapi_login&state=" + UUID.randomUUID());
        HttpGet get = new HttpGet("https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxbbe8b9496e8ed462&redirect_uri=https://auvg7x.39nat.com/web/token&response_type=code&scope=snsapi_userinfo&state=" + UUID.randomUUID());
        //执行
        HttpResponse response = httpClient.execute(get);
        //获取响应结果
        String result = EntityUtils.toString(response.getEntity());
        return result;
    }

    /**
     * 获取token
     *
     * @param code
     * @return
     */
    public static String getToken(String code) throws IOException {
        HttpClient httpClient = HttpClients.createDefault();
        HttpGet get = new HttpGet(" https://api.weixin.qq.com/sns/oauth2/access_token?appid=wxbbe8b9496e8ed462&secret=6c183f560ee60765cc303e7d121d18eb&code=" + code + "&grant_type=authorization_code");
        //执行
        HttpResponse response = httpClient.execute(get);
        //获取响应结果
        String result = EntityUtils.toString(response.getEntity());
        return result;
    }
}
